package com.example.news.fraction;

import com.example.news.ResourceTable;
import com.example.news.bean.JiSuNews;
import com.example.news.provider.Newsprovider;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

public class GuoJi extends Fraction {
    private ListContainer listContainer;
    private Newsprovider newsprovider;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_fraction_xinwen, container, false);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initView();

        new Thread(new Runnable() {
            @Override
            public void run() {
                zzrHttp();
            }
        }).start();
    }

    private void zzrHttp() {
        ZZRHttp.get(NewsDate.NewsList_guoji + NewsDate.APPKEY, new ZZRCallBack.CallBackString() {
            @Override
            public void onFailure(int i, String s) {
                System.out.println(s);
            }

            @Override
            public void onResponse(String s) {
                System.out.println(s);
                Gson gson = new Gson();
                JiSuNews news = gson.fromJson(s, JiSuNews.class);
                listContainer.setItemProvider(new Newsprovider(news.getResult().getList(),getFractionAbility()));
            }
        });

    }

    private void initView() {
        listContainer = getFractionAbility().findComponentById(ResourceTable.Id_lc);
    }
}
